<template>
	<view class="">
		<view class="cu-card dynamic">
			<view class="cu-item shadow">
				<view class="input-row border">
					<text class="title title-require start-mark">车牌号：</text>
					<picker @change="bindPickerChange" class="cuspicker" style="margin-left: 5px;"
					 range-key="LicensePlateNO" :value="ch_index" :range="car_list"
					 data-type="car">
						<view class="picker cuspicker_item">
							{{car_list[ch_index.car].LicensePlateNO||''}}
						</view>
					</picker>
					<!-- <m-input type="text" v-model="driverInfo.carNo" placeholder="请输入车牌号"></m-input> -->
				</view>
				<view class="input-row border">
					<text class="title title-require start-mark" style="width: 162upx;">姓名：</text>	
					 <m-input type="text" v-model="driverInfo.name" placeholder="请输入姓名"></m-input> 
				</view>
		
				<view class="input-row border">
					<text class="title title-require start-mark" style="width: 162upx;">手机号：</text>
					<m-input type="text" v-model="driverInfo.phone" placeholder="请输入手机号"></m-input>
				</view>
				<view class="input-row border">
					<text class="title title-require start-mark">装货仓库：</text>
					<picker mode = 'selector'  :range="wareHouseArr" :value="index" data-type='0'  range-key="DataCode"
					@change='wareHouseChange' style="margin-left: 10px;" class="cuspicker"
					> 
					  <view class="">
					  	{{wareHouseArr[index].DataCode||'' }}
					  </view>
					</picker>
				</view>
				<view class="input-row border">
					<text class="title title-require start-mark">进出标记：</text>
					<picker mode = 'selector'  :range="inOrOutArr" :value="index1" data-type='1'
					@change='wareHouseChange' style="margin-left: 10px;" class="cuspicker"
					> 
					  <view class="">
					  	{{inOrOutArr[index1]}}
					  </view>
					</picker>
				</view>
				<view class="input-row border" style="overflow: hidden;text-align: center;">
					<text class="title title-require ">送货地址：</text>
					<textarea type="text" class="textArea" v-model="driverInfo.address"></textarea>
				</view>
			</view>
			
			<view class="cu-item shadow" style="text-align: center;">
				<view class="flex justify-center"  style="height: 100upx;margin-top: 15px;">
					<button class="cu-btn bg-blue " style="width: 40%;background-color: #337AB7;" @click="submit" >提交</button>
					<!-- <button class="cu-btn bg-green " style="width: 20%;background-color: #337AB7;margin-left: 30px;" @click="reset" >重置</button> -->
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import mInput from '@/components/m-input.vue';
export default {
	components: {
		mInput
	},
	data() {
		return {
			ch_index: {
				car: 0
			},
			car_list: [{
				LicensePlateNO: ''
			}],
			driverInfo: {
				carNo: '',
				name: '',
				phone: '',
				
				address: ''
			},
			index:0,
			index1:0,
			inOrOutArr:['进区','出区'],  //进区  I 出区E
			wareHouseArr:[
				/* {code:0,value:'1号库'},
				{code:1,value:'2号库'},
				{code:2,value:'4号库'},
				{code:3,value:'5号库'},
				{code:4,value:'6号库'},
				{code:5,value:'保税仓'}, */
			],
			session:{},
			userInfo:{},
		};
	},
	mounted(){
		uni.showLoading({
		    title: '加载中'
		});
		this.session=uni.getStorageSync('session');
		this.userInfo=this.$store.state.uni.UserInfo || {};
	//	console.log('this.userInfo-9-',this.userInfo)
	 this.driverInfo.name=this.userInfo.RealName
	 this.driverInfo.phone=this.userInfo.Phone
	 
		this.$store.dispatch('uni/GetTruckList', {
				openid: this.session.openid
			})
			.then(res => {
				/* uni.setStorage({
					key: 'truckList',
					data: res.data,
					success: () => {}
				}) */
				this.car_list = res.Data || [];
				this.driverInfo.carNo=this.car_list[this.ch_index.car].LicensePlateNO
				uni.hideLoading()
			}).catch(()=>{
				uni.hideLoading()
			})
			
		this.getwareHouseArr()
	},
	methods: {
		/* GetDriverMessageList(){
			console.log('2222222')
			this.$store.dispatch('uni/GetDriverMessageList',{
				openid:this.session.openid,
				query:true
			}).then(res=>{
				console.log('res-0000---',res)
			})
		}, */
		getwareHouseArr(){
			this.$store.dispatch('uni/Config',{
				dataType:'6'
			}).then(res=>{
				this.wareHouseArr=res
			})
		},
		bindPickerChange(e) {
			const {
				type
			} = e.currentTarget.dataset;
			this.ch_index[type] = e.detail.value;
			
			this.driverInfo.carNo=this.car_list[this.ch_index.car].LicensePlateNO
		},
		wareHouseChange(value){
			const {type}=value.currentTarget.dataset
			if(type==='0'){
				this.index=value.detail.value
				this.driverInfo.warehouse=this.wareHouseArr[value.detail.value].DataCode
			}else if(type==='1'){
				this.index1=value.detail.value
				this.driverInfo.inOrOut=this.inOrOutArr[this.index1]
			}
		},
		/* reset(){
			this.driverInfo={}
			
			this.index=0
			this.index1=0
		}, */
		submit(){
			console.log('this.driverInfo====',this.driverInfo)
			if(this.driverInfo.phone){
				 if(!(/^1[3456789]\d{9}$/.test(this.driverInfo.phone))){ 
				    uni.showToast({
				    	title:'请填写正确的手机号码',
				    	icon:'none'
				    })
				     return; 
				    } 
			}
			if(!this.driverInfo.carNo||!this.driverInfo.name||!this.driverInfo.phone){
				uni.showToast({
					title:'请填写司机个人信息',
					icon:'none'
				})
				return;
			}
			
			let params={}
			const Turnover=['I','E']
			params.OpenID=this.session.openid
			params.VehicleNO=this.driverInfo.carNo
			params.DriverName=this.driverInfo.name
			params.DriverNumber=this.driverInfo.phone
			params.LoadingWareHouse=this.wareHouseArr[this.index].DataCode
			params.Address=this.driverInfo.address
			params.Turnover=Turnover[this.index1]
			console.log('params---',params)
			this.$store.dispatch('uni/AddDriverMessage',params).then(res=>{
				console.log(typeof res.Code,res)
				if(res.Code===0){
					uni.showToast({
						title:res.Message,
					})
					setTimeout(()=>{
						uni.navigateBack({
							delta:1
						})
					},800)
				}else{
					uni.showToast({
						title:res.Message,
						icon:'none'
					})
				}
			}).catch()
		}
	}
};
</script>

<style lang="less">
.cu-item {
	padding-left: 15px;
	.title{
		width: 40%;
	}
	.textArea {
		
		
		padding-top: 10px;
		height: 60px;
		min-height: 30px;
		//text-align: left;
		white-space: normal;
		text-align: justify;
		text-decoration: underline;
		//text-align-last: center;
	}
}
/* .cu-item .input-row:nth-of-type(odd){
		background-color: #E8F0FE;
	} */
.start-mark::before {
	content: '*';
	color: red;
	position: absolute;
	//top: 7px;
	left: 6px;
	font-size: 20px;
}
.cuspicker{
	width: 100%;
}
/* .cuspicker::after{
	content: '>';
	color: #78C0E6;
	position: absolute;
	right: 18px;
	font-size: 20px;
	top:9px;
} */
</style>
